<template>
  <div>
    <el-card>
      <el-row type="flex" justify="end">
        <el-button
          type="primary"
          size="mini"
          @click="addFn('0','1')"
        >添加权限</el-button>
      </el-row>
      <el-divider />
      <!-- 表格部分 -->
      <el-table
        :data="permissionsList"
        row-key="id"
        border
        style="width: 100%"
        :tree-props="{children: 'children'}"
      >
        <el-table-column label="权限名称" width="240" prop="title" />
        <el-table-column label="权限标识" width="240" prop="code" />
        <el-table-column label="权限描述" prop="description" />
        <el-table-column label="操作">
          <template v-slot="{ row }">
            <el-button
              v-if="row.pid==='0'"
              size="mini"
              type="success"
              @click="addFn(row.id,'2')"
            >添加子权限</el-button>
            <el-button
              size="mini"
              type="warning"
              @click="editFn(row.id)"
            >编辑</el-button>
            <el-button
              size="mini"
              type="danger"
              @click="delFn(row.id)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 弹出框 -->
    <addPerm ref="addPrem" :show-dialog.sync="showDialog" @updateEvent="getPermissionList" />
  </div>
</template>

<script>
import { getPermissionList, delPermission } from '@/api/permissions'
import addPerm from './components/add-perm.vue'
export default {
  components: { addPerm },
  data() {
    return {
      permissionsList: [],
      // 弹出框
      showDialog: false
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    async getPermissionList() {
      const res = await getPermissionList()
      // console.log(53, res.data)
      this.permissionsList = this.listToTree(res.data, '0')
      // console.log(61, this.permissionsList)
      // this.permissionsList.forEach(item => {
      //   if (item.children.length !== 0) {
      //     item.hasChildren = true
      //   }
      // })
    },
    // 列表转树形
    listToTree(list, id) {
      const arr = []
      // 转换过程
      list.forEach(item => {
        // 获取一级列表,返回的id是number类型,pid是字符串类型,所以需要转换
        if (item.pid === id + '') {
          arr.push(item)

          // 把该数组赋值给一级部门的children属性即可
          item.children = this.listToTree(list, item.id)
          // console.log(119, res)// res就是二级部门的数组
        }
      })
      return arr
    },

    // 添加权限
    addFn(pid, type) {
      this.showDialog = true
      this.$refs.addPrem.formData.pid = pid + ''
      this.$refs.addPrem.formData.type = type
    },
    // 编辑权限
    editFn(id) {
      this.showDialog = true
      // 调用子组件实现数据回显
      this.$refs.addPrem.getPermissionDetail(id)
    },
    // 删除权限
    delFn(id) {
      this.$confirm('是否确定删除该条数据', '删除权限', {
        type: 'warning'
      }).then(async() => {
        await delPermission(id)
        // 重新渲染
        this.getPermissionList()
      }).catch(() => {})
    }
  }
}
</script>

<style lang="scss" scoped>
.el-card {
  margin: 20px;
}
</style>
